﻿using Book_WebAPI.Models;
using Book_WebAPI.Services;
using Microsoft.AspNetCore.Mvc;

namespace Book_WebAPI.Controllers
{
    [Route("api/author/books")]
    [ApiController]
    public class BookController:ControllerBase
    {
        // 依赖注入-通用仓储接口
        public IAuthorRepository AuthorRepository { get; }
        public IBookRepository BookRepository { get; }
        public BookController(IBookRepository bookRepository, IAuthorRepository authorRepository)
        {
            AuthorRepository = authorRepository;
            BookRepository = bookRepository;
        }

        /// <summary>
        /// 获取图书详情
        /// </summary>
        /// <param name="authorId"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<Book>> GetBooks(int authorId)
        {
            if (!AuthorRepository.IsAuthorExists(authorId))
            {
                return NotFound();
            }
            return BookRepository.GetBookForAuthor(authorId).ToList();
        }

        /// <summary>
        /// 通过id获取指定图书
        /// </summary>
        /// <param name="id"></param>
        /// <param name="authorId"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<Book> GetBooKMsg(int id, int authorId)
        {
            if (!AuthorRepository.IsAuthorExists(authorId))
            {
                return NotFound("该作者并不存在！");
            }
            var book = BookRepository.GetBookForAuthor(id, authorId);
            if (book == null)
            {
                return NotFound();
            }
            return book;
        }

        /// <summary>
        /// 新增图书
        /// </summary>
        /// <param name="authorId"></param>
        /// <param name="bookCreate"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddBook(int authorId, BookCreate bookCreate)
        {
            if(!AuthorRepository.IsAuthorExists(authorId)) { return NotFound(); }
            var book = new Book
            {
                id = bookCreate.id,
                title = bookCreate.title,
                description = bookCreate.description,
                pages = bookCreate.pages,
                authorId = authorId,
            };
            BookRepository.AddBook(book);
            return CreatedAtRoute(nameof(AddBook), new {authorId =  authorId, id = book.id}, book);
        }

        /// <summary>
        /// 通过id获取最新添加的图书
        /// </summary>
        /// <param name="id"></param>
        /// <param name="authorId"></param>
        /// <returns></returns>
        [HttpGet("/GetNewAddBook/{id}", Name = nameof(AddBook))]
        public ActionResult<Book> GetNewAddBook(int id, int authorId)
        {
            var book = BookRepository.GetBookForAuthor(id, authorId);
            if (book == null)
            {
                return NotFound();
            }
            else
            {
                return book;
            }
        }

        /// <summary>
        /// 根据id删除指定图书
        /// </summary>
        /// <param name="authorId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult DeleteBook(int authorId, int id)
        {
            if (!AuthorRepository.IsAuthorExists(authorId)) { return NotFound(); }
            var book = BookRepository.GetBookForAuthor(id, authorId);
            if(book == null)
            {
                return NotFound();
            }
            BookRepository.DeleteBook(book);
            return Ok("删除成功");
        }

        /// <summary>
        /// 更新图书
        /// </summary>
        /// <param name="authorId"></param>
        /// <param name="id"></param>
        /// <param name="bookUpdate"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public IActionResult UpdateBook(int authorId, int id, BookUpdate bookUpdate)
        {
            if (!AuthorRepository.IsAuthorExists(authorId)) { return NotFound(); }
            var book = BookRepository.GetBookForAuthor(id, authorId);
            if(book == null)
            {
                return NotFound();
            }
            BookRepository.UpdateBook(authorId, id, bookUpdate);
            return Ok("更新成功");
        }

    }
}
